package controller.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import controller.decoders.EventDecoder;

/*
 * Defines the type and some required instance variables for
 * ActionListener objects
 */
public abstract class AbstractActionListener implements ActionListener
{
	// Maintains references to an ActionEvent field, and a worker thread
	// that instantiates EventDecoder
	protected ActionEvent event;
	protected EventDecoder eventDecoder;
	protected Thread worker;
	
	public synchronized void actionPerformed(ActionEvent e)
	{
		synchronized( eventDecoder )
		{
			// Register an ActionEvent and notify the acting EventDecoder thread
			this.event = e;
			eventDecoder.notify();
		}
	}
	
	public void startWorker()
	{
		// Start a worker thread
		this.worker = new Thread(this.eventDecoder);
		this.worker.start();
	}
	
	// Kill this listener's worker thread
	public void killTheMotherfucker()
	{
		worker.interrupt();
		System.out.println("[CONTROLLER] Worker thread terminated! ");
	}
	
	/*
	 * ---------
	 * Accessors
	 * --------- 
	 */
	public ActionEvent getActionEvent()
	{
		return event;
	}
	
	public EventDecoder getEventDecoder()
	{
		return eventDecoder;
	}
	
	public String toString()
	{
		return this.getClass().getSimpleName();
	}
}
